<?php

namespace App\Http\Controllers\Resources;

use App\Models\Group;
use App\Models\Project;
use App\Models\WxClient;
use App\Services\ServiceFactory;
use Illuminate\Http\Request;
use Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProjectRequest;
use App\Services\Interfaces\RepoServiceInterface;

class ProjectController extends Controller
{
    public function __construct($_resource)
    {
        $this->_resource = $_resource;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //当前登录用户为操作员的话,无权限创建
        //$this->authorize(new Project());
        $res_data = $this->_repoService()->read();
        return $res_data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('create', new Project());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProjectRequest $request)
    {
        $group_id = $request->all()['group_id'];
        $wx_id = $request->all()['wx_id'];
        $group = Group::find($group_id);
        if (!$wx_id) {
            return $this->_error('wx_id不可为空');
        }
        if (is_null($group)) {
            return $this->_error('该业务结构不存在');
        }
        if (isset($group->children) && !empty($group->children)) {
            return $this->_error('该业务结构无法创建项目');
        }
        $project = Project::where('group_id', $group_id)->count();
        if ($project) {
            return $this->_error('项目已存在');
        }
        $wx_id = WxClient::where([
            'company_id' => $group->company_id,
            'project_id' => ['$in' => [[],'',null]],
        ])->whereIn('_id',  $wx_id)
            ->get()->pluck('id')->all();
        if (!$wx_id) {
            return $this->_error('微信号不可用');
        }
        $data = [];
        $data['group_id'] = $group_id;
        $data['title'] = $group->name;
        $data['users'] = $this->_repoService()->getUsers($group_id);
        $data['wx_id'] = $wx_id;
        $result = $this->_repoService()->create($data);
        return $this->_repoService()->find(['id', $result->_id]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $result = $this->_repoService()->find(['id', $id]);
        if ($result->_id) {
            return result;
        }
        return $this->_error('该项目不存在');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('edit', new Project());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->all();
        $project = Project::find($id);
        if (is_null($project)) {
            return $this->_error('该业务结构不存在');
        }
        $filter = ['id' => $project->id];
        $isSave = $this->_repoService()->update($filter, $data);
        if ($isSave) {
            return $this->_repoService()->find(['id', $id]);
        } else {
            return $this->_error('保存失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('destroy', new Project());
    }
}
